package utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import acessoDados.Singleton;
import acessoDados.dao.BaseDAO;

import constants.ProjectConstants;

public class MetodosAuxiliares {
	public static String getTipoFuncao(int index){
		switch(index){
		case 1: return "Cliente";
		case 2: return "Tripulante";
		case 3: return "Funcionario";
		case 4: return "Gestor";
		default: return "Unknown";
		}
	}
	
	public static int getIndiceFuncao(String funcao){
		if(funcao.equalsIgnoreCase("Cliente"))
			return 1;
		else if(funcao.equalsIgnoreCase("Tripulante"))
			return 2;
		else if(funcao.equalsIgnoreCase("Funcionario"))
			return 3;
		else if(funcao.equalsIgnoreCase("Gestor"))
			return 4;
		else return 1;
	}
	
	public static boolean containsItem (String[] s, String item){
		for(int i=0; i<s.length; i++)
			if(s[i].equalsIgnoreCase(item))
				return true;
		
		return false;
	}
	
	public static int calculaMilhas(String idViagem) throws SQLException, ClassNotFoundException{

		int totalMilhas=0;
		int coordXAnt=ProjectConstants.UEMELX;
		int coordYAnt=ProjectConstants.UEMELY;
		int coordX, coordY;
		ResultSet result = Singleton.getInstance().getViagemDAO().getLocaisViagem(idViagem);
		while(result.next()){
			coordX = result.getInt("coordX");
			coordY = result.getInt("coordY");
			totalMilhas += Math.round( Math.sqrt(Math.pow((coordX - coordXAnt),2) + Math.pow((coordY - coordYAnt),2)) );
//			System.out.println("Local anterior:"+coordXAnt+","+coordYAnt+" local:"+coordX+","+coordY+" total:"+totalMilhas);
//			System.out.println("x-x ^2:" + ((Math.pow((coordX - coordXAnt),2))));
//			System.out.println("y-y ^2:" + (Math.pow((coordY - coordYAnt),2)));
//			System.out.println("raiz: " + Math.sqrt(Math.pow((coordX - coordXAnt),2) + Math.pow((coordY - coordYAnt),2)));
//			System.out.println("round: " + Math.round( Math.sqrt(Math.pow((coordX - coordXAnt),2) + Math.pow((coordY - coordYAnt),2))));
			coordXAnt = coordX;
			coordYAnt = coordY;
		}
		totalMilhas += Math.round( Math.sqrt(Math.pow((ProjectConstants.UEMELX - coordXAnt),2) + Math.pow((ProjectConstants.UEMELY - coordYAnt),2)) );
//		System.out.println("Local anterior:"+coordXAnt+","+coordYAnt+" total:"+totalMilhas);
//		System.out.println("x-x ^2:" + ((Math.pow((ProjectConstants.UEMELX - coordXAnt),2))));
//		System.out.println("y-y ^2:" + (Math.pow((ProjectConstants.UEMELY - coordYAnt),2)));
//		System.out.println("raiz: " + Math.sqrt(Math.pow((ProjectConstants.UEMELX - coordXAnt),2) + Math.pow((ProjectConstants.UEMELY - coordYAnt),2)));
//		System.out.println("round: " + Math.round( Math.sqrt(Math.pow((ProjectConstants.UEMELX - coordXAnt),2) + Math.pow((ProjectConstants.UEMELY - coordYAnt),2))));
		
		return totalMilhas;
	}
	
//	public static void main(String[] args) throws SQLException, ClassNotFoundException{
//		calculaMilhas("10");
//	}
}
